<?php


namespace app\common\model;


use think\Model;

class CouponGenerate extends Model
{
    protected $table = "maniac_coupon_generate";
    protected $createTime = "created_at";
    protected $updateTime = "updated_at";
    public $autoWriteTimestamp = "datetime";

    public function coupon(){
        return $this->hasOne('app\common\model\Coupon','id','coupon_id');
    }
    //员工姓名
    public function getStaffNameAttr($value,$data){
        return $data['staff_id'] ? MemberQy::where("id",$data['staff_id'])->value('name') : "管理员";
    }
    public function getPendAttr($value,$data){
        return Coupon::withTrashed()->where("id",$data['coupon_id'])->value('pend');
    }
    //获取数量
    public function getYgNumberAttr($value,$data){
        return self::where("coupon_id",$data['coupon_id'])->where("receive_status",0)->where('staff_id',$data['staff_id'])->count('id');
    }
    //开始时间
    public function getCouponPbeginAttr($value,$data){
        return date('Y-m-d H:i',Coupon::withTrashed()->where("id",$data['coupon_id'])->value('pbegin'));
    }
    //结束时间
    public function getCouponPendAttr($value,$data){
        return date('Y-m-d H:i',Coupon::withTrashed()->where("id",$data['coupon_id'])->value('pend'));
    }

    //优惠券类型
    public function getCouponTypeAttr($value,$data){
        $status = [0=>'满减劵',1=>'抵用券'];
        $type = Coupon::withTrashed()->where("id",$data['coupon_id'])->value('coupon_type');
        return $status[$type];
    }
    //员工领取数量
    public function getStaffReceiveNumbersAttr($value,$data){
        return self::where("staff_id",$data['staff_id'])->where("type",1)->where("coupon_id",$data['coupon_id'])
            ->where("is_release",1)->count('id');
    }
    //员工已使用数量
    public function getStaffUsedNumbersAttr($value,$data){
        $coupon = Coupon::withTrashed()->field("id")->order('id')->select();
        foreach ($coupon as $key => $value){
            $coupon[$key]['count'] = self::where("staff_id",$data['staff_id'])->where("coupon_id",$value['id'])
                ->where("status",1)->count('id');
        }
        return $coupon;
    }
    //获取核销记录
    public function getVerificationInfoAttr($value,$data){
        return VerificationLog::where("verification_coupon_id",$data['id'])
            ->append(['staff_name','coupon_name','member_name'])->find();
    }
}